package com.example.rfidwms;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private Application app = new Application();

    private EditText editTextUserid;
    private EditText editTextUserPassword;
    private Button buttonUserLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //对控件进行初始化操作
        initView();
    }

    private void initView() {
        editTextUserid = findViewById(R.id.editText_userid);
        editTextUserPassword = findViewById(R.id.editText_userPassword);
        buttonUserLogin = findViewById(R.id.button_userLogin);
    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            JSONObject json = (JSONObject)msg.obj;
            try {
                if (json.getString("result").equals("登录成功")) {

//                  得到登录用户的信息，给app的公共变量赋值。
                    app.user_id = json.getString("id");
                    app.user_name = json.getString("name");
                    app.dpt_id = json.getString("dpt_id");
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class); // 创建一个 Intent 对象
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this, json.getString("result"), Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    public void login(View view) {
        //得到用户输入的信息,进行非空判断
        String id = editTextUserid.getText().toString().trim();
        String password = editTextUserPassword.getText().toString().trim();
        if (TextUtils.isEmpty(id) || TextUtils.isEmpty(password)) {
            Toast.makeText(LoginActivity.this, "不能输入为空", Toast.LENGTH_SHORT).show();
            return;
        } else {
            OkHttpClient okHttpClient = new OkHttpClient();
            FormBody formBody = new FormBody.Builder()
                    .add("id", id).add("password", password)
                    .build();

            Request request = new Request.Builder()
                    .post(formBody)
                    .url(app.path_login)
                    .build();

            Call call = okHttpClient.newCall(request);

            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    runOnUiThread(() -> Toast.makeText(LoginActivity.this, "HTTP响应失败", Toast.LENGTH_SHORT).show());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String string = response.body().string();

                    try {
                        JSONArray jsonArray = new JSONArray(string);
                        Message msg = Message.obtain();

                        msg.obj = jsonArray.getJSONObject(0);

                        handler.sendMessage(msg);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

}